/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ensma.ia.miniblog.persistence;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author LeBlero
 */
@Entity
public class Commentaire implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name="texte",nullable=false)
    private String texte;
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="Article_FK")
    private Article article;
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="Auteur_FK")
    private Lecteur auteur;
    @Temporal(TemporalType.DATE)
    private Date datePost;

    public Commentaire() {
        article = null;
        auteur = null;
        texte = "";
        Calendar c = new GregorianCalendar();
        datePost = c.getTime();
    }

    public Commentaire(Article a, Lecteur l, String t) {
        this.article = a;
        this.auteur = l;
        this.texte = t;
        Calendar c = new GregorianCalendar();
        datePost = c.getTime();
    }

    public Long getId() {
        return id;
    }

    public String getTexte() {
        return texte;
    }

    public void setTexte(String texte) {
        this.texte = texte;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }

    public Lecteur getAuteur() {
        return auteur;
    }

    public void setAuteur(Lecteur auteur) {
        this.auteur = auteur;
    }

    public Date getDatePost() {
        return datePost;
    }

    public void setDatePost(Date datePost) {
        this.datePost = datePost;
    }

    @PrePersist
    private void valideAvantPersistance() {
        if (id != null || texte == null || datePost == null) {
            System.out.println("Commentaire::PrePersist::Erreur avant persistance");
        }
    }

    @PostPersist
    private void valideApresPersistance() {
        if (id == null) {
           System.out.println("Commentaire::PostPersist::Erreur durant persistance");
        }
    }

    @PreUpdate
    @PreRemove
    private void valideAvantUpdateOrRemove() {
        if (id == null) {
            System.out.println("Commentaire::PreUpdateRemove:Update ou remove impossible, ID null");
        }
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Commentaire)) {
            return false;
        }
        Commentaire other = (Commentaire) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "fr.ensma.ia.miniblog.Commentaire[id=" + id + "]";
    }

}
